<?php
class Db_Provider
{
    public static function total($condition=false) {
        static $totals;
        $key = hash('crc32b', $condition);
        if(!isset($totals[$key]))
            $totals[$key] = DB::count('provider', $condition, __FILE__ . ' line ' . __LINE__);
        return $totals[$key];
    }

    public static function getProviders($limit='', $condition='')
    {
        static $providers;
        $key = hash('crc32b', $condition.$limit);
        if(!isset($providers[$key]))
        {
            if($condition)
            {
            	$condition = 'WHERE '.$condition;
            }
            if($limit)
            {
            	$limit = 'LIMIT '.$limit;
            }
            $providers[$key] =  DB::fetch_all("SELECT * FROM provider $condition $limit", __FILE__ . ' line ' . __LINE__);
        }
        return $providers[$key];
    }

    public static function selectProviders($cols='*', $limit='', $condition='') {
        static $providers;
        $key = hash('crc32b', $cols.$condition.$limit);
        if(!isset($providers[$key])) {
            if($condition) $condition = 'WHERE '.$condition;
            $providers[$key] =  DB::fetch_all("SELECT $cols FROM provider $condition $limit", __FILE__ . ' line ' . __LINE__);
        }
        return $providers[$key];
    }

    public static function getProviderById($id)
    {
        static $provider;
        if(!isset($provider[$id]))
        {
        	$provider[$id] = DB::select('provider', 'id='.$id, __FILE__ . ' line ' . __LINE__);
        }
        return $provider[$id];
    }

    public static function insert($provider) {
        return DB::insert('provider', $provider, false, __FILE__ . ' line ' . __LINE__);
    }

    public static function update($provider, $id)
    {
        return DB::update_id('provider', $provider, $id, __FILE__ . ' line ' . __LINE__);
    }

    public static function delProviderById($id)
    {
        return DB::update_id('provider', array("status" => -1), $id, __FILE__ . ' line ' . __LINE__);
    }

    public static function getImageUrl($url)
    {
        return IMAGE_PATH.'/'.$url;
    }
}
